home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / perl5 / HTML / Entities.pm next >
Text File  |  2009-02-09  |  15KB  |  491 lines

  1. package HTML::Entities;
  2.  
  3. =head1 NAME
  4.  
  5. HTML::Entities - Encode or decode strings with HTML entities
  6.  
  7. =head1 SYNOPSIS
  8.  
  9.  use HTML::Entities;
  10.  
  11.  $a = "Våre norske tegn bør æres";
  12.  decode_entities($a);
  13.  encode_entities($a, "\200-\377");
  14.  
  15. For example, this:
  16.  
  17.  $input = "vis-α-vis BeyoncΘ's na∩ve\npapier-mΓchΘ rΘsumΘ";
  18.  print encode_entities($input), "\n"
  19.  
  20. Prints this out:
  21.  
  22.  vis-à-vis Beyoncé's naïve
  23.  papier-mâché résumé
  24.  
  25. =head1 DESCRIPTION
  26.  
  27. This module deals with encoding and decoding of strings with HTML
  28. character entities.  The module provides the following functions:
  29.  
  30. =over 4
  31.  
  32. =item decode_entities( $string, ... )
  33.  
  34. This routine replaces HTML entities found in the $string with the
  35. corresponding Unicode character.  Under perl 5.6 and earlier only
  36. characters in the Latin-1 range are replaced. Unrecognized
  37. entities are left alone.
  38.  
  39. If multiple strings are provided as argument they are each decoded
  40. separately and the same number of strings are returned.
  41.  
  42. If called in void context the arguments are decoded in-place.
  43.  
  44. This routine is exported by default.
  45.  
  46. =item _decode_entities( $string, \%entity2char )
  47.  
  48. =item _decode_entities( $string, \%entity2char, $expand_prefix )
  49.  
  50. This will in-place replace HTML entities in $string.  The %entity2char
  51. hash must be provided.  Named entities not found in the %entity2char
  52. hash are left alone.  Numeric entities are expanded unless their value
  53. overflow.
  54.  
  55. The keys in %entity2char are the entity names to be expanded and their
  56. values are what they should expand into.  The values do not have to be
  57. single character strings.  If a key has ";" as suffix,
  58. then occurrences in $string are only expanded if properly terminated
  59. with ";".  Entities without ";" will be expanded regardless of how
  60. they are terminated for compatibility with how common browsers treat
  61. entities in the Latin-1 range.
  62.  
  63. If $expand_prefix is TRUE then entities without trailing ";" in
  64. %entity2char will even be expanded as a prefix of a longer
  65. unrecognized name.  The longest matching name in %entity2char will be
  66. used. This is mainly present for compatibility with an MSIE
  67. misfeature.
  68.  
  69.    $string = "foo bar";
  70.    _decode_entities($string, { nb => "@", nbsp => "\xA0" }, 1);
  71.    print $string;  # will print "fooábar"
  72.  
  73. This routine is exported by default.
  74.  
  75. =item encode_entities( $string )
  76.  
  77. =item encode_entities( $string, $unsafe_chars )
  78.  
  79. This routine replaces unsafe characters in $string with their entity
  80. representation. A second argument can be given to specify which
  81. characters to consider unsafe (i.e., which to escape). The default set
  82. of characters to encode are control chars, high-bit chars, and the
  83. C<< < >>, C<< & >>, C<< > >>, C<< ' >> and C<< " >>
  84. characters.  But this, for example, would encode I<just> the
  85. C<< < >>, C<< & >>, C<< > >>, and C<< " >> characters:
  86.  
  87.   $encoded = encode_entities($input, '<>&"');
  88.  
  89. This routine is exported by default.
  90.  
  91. =item encode_entities_numeric( $string )
  92.  
  93. =item encode_entities_numeric( $string, $unsafe_chars )
  94.  
  95. This routine works just like encode_entities, except that the replacement
  96. entities are always C<&#xI<hexnum>;> and never C<&I<entname>;>.  For
  97. example, C<encode_entities("r\xF4le")> returns "rôle", but
  98. C<encode_entities_numeric("r\xF4le")> returns "rôle".
  99.  
  100. This routine is I<not> exported by default.  But you can always
  101. export it with C<use HTML::Entities qw(encode_entities_numeric);>
  102. or even C<use HTML::Entities qw(:DEFAULT encode_entities_numeric);>
  103.  
  104. =back
  105.  
  106. All these routines modify the string passed as the first argument, if
  107. called in a void context.  In scalar and array contexts, the encoded or
  108. decoded string is returned (without changing the input string).
  109.  
  110. If you prefer not to import these routines into your namespace, you can
  111. call them as:
  112.  
  113.   use HTML::Entities ();
  114.   $decoded = HTML::Entities::decode($a);
  115.   $encoded = HTML::Entities::encode($a);
  116.   $encoded = HTML::Entities::encode_numeric($a);
  117.  
  118. The module can also export the %char2entity and the %entity2char
  119. hashes, which contain the mapping from all characters to the
  120. corresponding entities (and vice versa, respectively).
  121.  
  122. =head1 COPYRIGHT
  123.  
  124. Copyright 1995-2006 Gisle Aas. All rights reserved.
  125.  
  126. This library is free software; you can redistribute it and/or
  127. modify it under the same terms as Perl itself.
  128.  
  129. =cut
  130.  
  131. use strict;
  132. use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION);
  133. use vars qw(%entity2char %char2entity);
  134.  
  135. require 5.004;
  136. require Exporter;
  137. @ISA = qw(Exporter);
  138.  
  139. @EXPORT = qw(encode_entities decode_entities _decode_entities);
  140. @EXPORT_OK = qw(%entity2char %char2entity encode_entities_numeric);
  141.  
  142. $VERSION = "3.60";
  143. sub Version { $VERSION; }
  144.  
  145. require HTML::Parser;  # for fast XS implemented decode_entities
  146.  
  147.  
  148. %entity2char = (
  149.  # Some normal chars that have special meaning in SGML context
  150.  amp    => '&',  # ampersand 
  151. 'gt'    => '>',  # greater than
  152. 'lt'    => '<',  # less than
  153.  quot   => '"',  # double quote
  154.  apos   => "'",  # single quote
  155.  
  156.  # PUBLIC ISO 8879-1986//ENTITIES Added Latin 1//EN//HTML
  157.  AElig    => chr(198),  # capital AE diphthong (ligature)
  158.  Aacute    => chr(193),  # capital A, acute accent
  159.  Acirc    => chr(194),  # capital A, circumflex accent
  160.  Agrave    => chr(192),  # capital A, grave accent
  161.  Aring    => chr(197),  # capital A, ring
  162.  Atilde    => chr(195),  # capital A, tilde
  163.  Auml    => chr(196),  # capital A, dieresis or umlaut mark
  164.  Ccedil    => chr(199),  # capital C, cedilla
  165.  ETH    => chr(208),  # capital Eth, Icelandic
  166.  Eacute    => chr(201),  # capital E, acute accent
  167.  Ecirc    => chr(202),  # capital E, circumflex accent
  168.  Egrave    => chr(200),  # capital E, grave accent
  169.  Euml    => chr(203),  # capital E, dieresis or umlaut mark
  170.  Iacute    => chr(205),  # capital I, acute accent
  171.  Icirc    => chr(206),  # capital I, circumflex accent
  172.  Igrave    => chr(204),  # capital I, grave accent
  173.  Iuml    => chr(207),  # capital I, dieresis or umlaut mark
  174.  Ntilde    => chr(209),  # capital N, tilde
  175.  Oacute    => chr(211),  # capital O, acute accent
  176.  Ocirc    => chr(212),  # capital O, circumflex accent
  177.  Ograve    => chr(210),  # capital O, grave accent
  178.  Oslash    => chr(216),  # capital O, slash
  179.  Otilde    => chr(213),  # capital O, tilde
  180.  Ouml    => chr(214),  # capital O, dieresis or umlaut mark
  181.  THORN    => chr(222),  # capital THORN, Icelandic
  182.  Uacute    => chr(218),  # capital U, acute accent
  183.  Ucirc    => chr(219),  # capital U, circumflex accent
  184.  Ugrave    => chr(217),  # capital U, grave accent
  185.  Uuml    => chr(220),  # capital U, dieresis or umlaut mark
  186.  Yacute    => chr(221),  # capital Y, acute accent
  187.  aacute    => chr(225),  # small a, acute accent
  188.  acirc    => chr(226),  # small a, circumflex accent
  189.  aelig    => chr(230),  # small ae diphthong (ligature)
  190.  agrave    => chr(224),  # small a, grave accent
  191.  aring    => chr(229),  # small a, ring
  192.  atilde    => chr(227),  # small a, tilde
  193.  auml    => chr(228),  # small a, dieresis or umlaut mark
  194.  ccedil    => chr(231),  # small c, cedilla
  195.  eacute    => chr(233),  # small e, acute accent
  196.  ecirc    => chr(234),  # small e, circumflex accent
  197.  egrave    => chr(232),  # small e, grave accent
  198.  eth    => chr(240),  # small eth, Icelandic
  199.  euml    => chr(235),  # small e, dieresis or umlaut mark
  200.  iacute    => chr(237),  # small i, acute accent
  201.  icirc    => chr(238),  # small i, circumflex accent
  202.  igrave    => chr(236),  # small i, grave accent
  203.  iuml    => chr(239),  # small i, dieresis or umlaut mark
  204.  ntilde    => chr(241),  # small n, tilde
  205.  oacute    => chr(243),  # small o, acute accent
  206.  ocirc    => chr(244),  # small o, circumflex accent
  207.  ograve    => chr(242),  # small o, grave accent
  208.  oslash    => chr(248),  # small o, slash
  209.  otilde    => chr(245),  # small o, tilde
  210.  ouml    => chr(246),  # small o, dieresis or umlaut mark
  211.  szlig    => chr(223),  # small sharp s, German (sz ligature)
  212.  thorn    => chr(254),  # small thorn, Icelandic
  213.  uacute    => chr(250),  # small u, acute accent
  214.  ucirc    => chr(251),  # small u, circumflex accent
  215.  ugrave    => chr(249),  # small u, grave accent
  216.  uuml    => chr(252),  # small u, dieresis or umlaut mark
  217.  yacute    => chr(253),  # small y, acute accent
  218.  yuml    => chr(255),  # small y, dieresis or umlaut mark
  219.  
  220.  # Some extra Latin 1 chars that are listed in the HTML3.2 draft (21-May-96)
  221.  copy   => chr(169),  # copyright sign
  222.  reg    => chr(174),  # registered sign
  223.  nbsp   => chr(160),  # non breaking space
  224.  
  225.  # Additional ISO-8859/1 entities listed in rfc1866 (section 14)
  226.  iexcl  => chr(161),
  227.  cent   => chr(162),
  228.  pound  => chr(163),
  229.  curren => chr(164),
  230.  yen    => chr(165),
  231.  brvbar => chr(166),
  232.  sect   => chr(167),
  233.  uml    => chr(168),
  234.  ordf   => chr(170),
  235.  laquo  => chr(171),
  236. 'not'   => chr(172),    # not is a keyword in perl
  237.  shy    => chr(173),
  238.  macr   => chr(175),
  239.  deg    => chr(176),
  240.  plusmn => chr(177),
  241.  sup1   => chr(185),
  242.  sup2   => chr(178),
  243.  sup3   => chr(179),
  244.  acute  => chr(180),
  245.  micro  => chr(181),
  246.  para   => chr(182),
  247.  middot => chr(183),
  248.  cedil  => chr(184),
  249.  ordm   => chr(186),
  250.  raquo  => chr(187),
  251.  frac14 => chr(188),
  252.  frac12 => chr(189),
  253.  frac34 => chr(190),
  254.  iquest => chr(191),
  255. 'times' => chr(215),    # times is a keyword in perl
  256.  divide => chr(247),
  257.  
  258.  ( $] > 5.007 ? (
  259.   'OElig;'    => chr(338),
  260.   'oelig;'    => chr(339),
  261.   'Scaron;'   => chr(352),
  262.   'scaron;'   => chr(353),
  263.   'Yuml;'     => chr(376),
  264.   'fnof;'     => chr(402),
  265.   'circ;'     => chr(710),
  266.   'tilde;'    => chr(732),
  267.   'Alpha;'    => chr(913),
  268.   'Beta;'     => chr(914),
  269.   'Gamma;'    => chr(915),
  270.   'Delta;'    => chr(916),
  271.   'Epsilon;'  => chr(917),
  272.   'Zeta;'     => chr(918),
  273.   'Eta;'      => chr(919),
  274.   'Theta;'    => chr(920),
  275.   'Iota;'     => chr(921),
  276.   'Kappa;'    => chr(922),
  277.   'Lambda;'   => chr(923),
  278.   'Mu;'       => chr(924),
  279.   'Nu;'       => chr(925),
  280.   'Xi;'       => chr(926),
  281.   'Omicron;'  => chr(927),
  282.   'Pi;'       => chr(928),
  283.   'Rho;'      => chr(929),
  284.   'Sigma;'    => chr(931),
  285.   'Tau;'      => chr(932),
  286.   'Upsilon;'  => chr(933),
  287.   'Phi;'      => chr(934),
  288.   'Chi;'      => chr(935),
  289.   'Psi;'      => chr(936),
  290.   'Omega;'    => chr(937),
  291.   'alpha;'    => chr(945),
  292.   'beta;'     => chr(946),
  293.   'gamma;'    => chr(947),
  294.   'delta;'    => chr(948),
  295.   'epsilon;'  => chr(949),
  296.   'zeta;'     => chr(950),
  297.   'eta;'      => chr(951),
  298.   'theta;'    => chr(952),
  299.   'iota;'     => chr(953),
  300.   'kappa;'    => chr(954),
  301.   'lambda;'   => chr(955),
  302.   'mu;'       => chr(956),
  303.   'nu;'       => chr(957),
  304.   'xi;'       => chr(958),
  305.   'omicron;'  => chr(959),
  306.   'pi;'       => chr(960),
  307.   'rho;'      => chr(961),
  308.   'sigmaf;'   => chr(962),
  309.   'sigma;'    => chr(963),
  310.   'tau;'      => chr(964),
  311.   'upsilon;'  => chr(965),
  312.   'phi;'      => chr(966),
  313.   'chi;'      => chr(967),
  314.   'psi;'      => chr(968),
  315.   'omega;'    => chr(969),
  316.   'thetasym;' => chr(977),
  317.   'upsih;'    => chr(978),
  318.   'piv;'      => chr(982),
  319.   'ensp;'     => chr(8194),
  320.   'emsp;'     => chr(8195),
  321.   'thinsp;'   => chr(8201),
  322.   'zwnj;'     => chr(8204),
  323.   'zwj;'      => chr(8205),
  324.   'lrm;'      => chr(8206),
  325.   'rlm;'      => chr(8207),
  326.   'ndash;'    => chr(8211),
  327.   'mdash;'    => chr(8212),
  328.   'lsquo;'    => chr(8216),
  329.   'rsquo;'    => chr(8217),
  330.   'sbquo;'    => chr(8218),
  331.   'ldquo;'    => chr(8220),
  332.   'rdquo;'    => chr(8221),
  333.   'bdquo;'    => chr(8222),
  334.   'dagger;'   => chr(8224),
  335.   'Dagger;'   => chr(8225),
  336.   'bull;'     => chr(8226),
  337.   'hellip;'   => chr(8230),
  338.   'permil;'   => chr(8240),
  339.   'prime;'    => chr(8242),
  340.   'Prime;'    => chr(8243),
  341.   'lsaquo;'   => chr(8249),
  342.   'rsaquo;'   => chr(8250),
  343.   'oline;'    => chr(8254),
  344.   'frasl;'    => chr(8260),
  345.   'euro;'     => chr(8364),
  346.   'image;'    => chr(8465),
  347.   'weierp;'   => chr(8472),
  348.   'real;'     => chr(8476),
  349.   'trade;'    => chr(8482),
  350.   'alefsym;'  => chr(8501),
  351.   'larr;'     => chr(8592),
  352.   'uarr;'     => chr(8593),
  353.   'rarr;'     => chr(8594),
  354.   'darr;'     => chr(8595),
  355.   'harr;'     => chr(8596),
  356.   'crarr;'    => chr(8629),
  357.   'lArr;'     => chr(8656),
  358.   'uArr;'     => chr(8657),
  359.   'rArr;'     => chr(8658),
  360.   'dArr;'     => chr(8659),
  361.   'hArr;'     => chr(8660),
  362.   'forall;'   => chr(8704),
  363.   'part;'     => chr(8706),
  364.   'exist;'    => chr(8707),
  365.   'empty;'    => chr(8709),
  366.   'nabla;'    => chr(8711),
  367.   'isin;'     => chr(8712),
  368.   'notin;'    => chr(8713),
  369.   'ni;'       => chr(8715),
  370.   'prod;'     => chr(8719),
  371.   'sum;'      => chr(8721),
  372.   'minus;'    => chr(8722),
  373.   'lowast;'   => chr(8727),
  374.   'radic;'    => chr(8730),
  375.   'prop;'     => chr(8733),
  376.   'infin;'    => chr(8734),
  377.   'ang;'      => chr(8736),
  378.   'and;'      => chr(8743),
  379.   'or;'       => chr(8744),
  380.   'cap;'      => chr(8745),
  381.   'cup;'      => chr(8746),
  382.   'int;'      => chr(8747),
  383.   'there4;'   => chr(8756),
  384.   'sim;'      => chr(8764),
  385.   'cong;'     => chr(8773),
  386.   'asymp;'    => chr(8776),
  387.   'ne;'       => chr(8800),
  388.   'equiv;'    => chr(8801),
  389.   'le;'       => chr(8804),
  390.   'ge;'       => chr(8805),
  391.   'sub;'      => chr(8834),
  392.   'sup;'      => chr(8835),
  393.   'nsub;'     => chr(8836),
  394.   'sube;'     => chr(8838),
  395.   'supe;'     => chr(8839),
  396.   'oplus;'    => chr(8853),
  397.   'otimes;'   => chr(8855),
  398.   'perp;'     => chr(8869),
  399.   'sdot;'     => chr(8901),
  400.   'lceil;'    => chr(8968),
  401.   'rceil;'    => chr(8969),
  402.   'lfloor;'   => chr(8970),
  403.   'rfloor;'   => chr(8971),
  404.   'lang;'     => chr(9001),
  405.   'rang;'     => chr(9002),
  406.   'loz;'      => chr(9674),
  407.   'spades;'   => chr(9824),
  408.   'clubs;'    => chr(9827),
  409.   'hearts;'   => chr(9829),
  410.   'diams;'    => chr(9830),
  411.  ) : ())
  412. );
  413.  
  414.  
  415. # Make the opposite mapping
  416. while (my($entity, $char) = each(%entity2char)) {
  417.     $entity =~ s/;\z//;
  418.     $char2entity{$char} = "&$entity;";
  419. }
  420. delete $char2entity{"'"};  # only one-way decoding
  421.  
  422. # Fill in missing entities
  423. for (0 .. 255) {
  424.     next if exists $char2entity{chr($_)};
  425.     $char2entity{chr($_)} = "&#$_;";
  426. }
  427.  
  428. my %subst;  # compiled encoding regexps
  429.  
  430. sub decode_entities_old
  431. {
  432.     my $array;
  433.     if (defined wantarray) {
  434.     $array = [@_]; # copy
  435.     } else {
  436.     $array = \@_;  # modify in-place
  437.     }
  438.     my $c;
  439.     for (@$array) {
  440.     s/(&\#(\d+);?)/$2 < 256 ? chr($2) : $1/eg;
  441.     s/(&\#[xX]([0-9a-fA-F]+);?)/$c = hex($2); $c < 256 ? chr($c) : $1/eg;
  442.     s/(&(\w+);?)/$entity2char{$2} || $1/eg;
  443.     }
  444.     wantarray ? @$array : $array->[0];
  445. }
  446.  
  447. sub encode_entities
  448. {
  449.     return undef unless defined $_[0];
  450.     my $ref;
  451.     if (defined wantarray) {
  452.     my $x = $_[0];
  453.     $ref = \$x;     # copy
  454.     } else {
  455.     $ref = \$_[0];  # modify in-place
  456.     }
  457.     if (defined $_[1] and length $_[1]) {
  458.     unless (exists $subst{$_[1]}) {
  459.         # Because we can't compile regex we fake it with a cached sub
  460.         my $code = "sub {\$_[0] =~ s/([$_[1]])/\$char2entity{\$1} || num_entity(\$1)/ge; }";
  461.         $subst{$_[1]} = eval $code;
  462.         die( $@ . " while trying to turn range: \"$_[1]\"\n "
  463.           . "into code: $code\n "
  464.         ) if $@;
  465.     }
  466.     &{$subst{$_[1]}}($$ref);
  467.     } else {
  468.     # Encode control chars, high bit chars and '<', '&', '>', ''' and '"'
  469.     $$ref =~ s/([^\n\r\t !\#\$%\(-;=?-~])/$char2entity{$1} || num_entity($1)/ge;
  470.     }
  471.     $$ref;
  472. }
  473.  
  474. sub encode_entities_numeric {
  475.     local %char2entity;
  476.     return &encode_entities;   # a goto &encode_entities wouldn't work
  477. }
  478.  
  479.  
  480. sub num_entity {
  481.     sprintf "&#x%X;", ord($_[0]);
  482. }
  483.  
  484. # Set up aliases
  485. *encode = \&encode_entities;
  486. *encode_numeric = \&encode_entities_numeric;
  487. *encode_numerically = \&encode_entities_numeric;
  488. *decode = \&decode_entities;
  489.  
  490. 1;
  491.